package com.jinznet.transit.filter;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jinznet.transit.util.Constant;
import com.jinznet.transit.util.CookieUtil;
import com.jinznet.transit.wx.WXJS;
  
public class WxOpenIdInterceptor implements HandlerInterceptor {  
  
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private static String JSAPITICKET = "jsApiTicket";
	
    @Override  
    public boolean preHandle(HttpServletRequest request,  
            HttpServletResponse response, Object handler) throws Exception {  
    	String sale_openid = CookieUtil.getCookieByKey(request, Constant.COOKIT_SALE_OPENID);
		if(StringUtils.isBlank(sale_openid)){
			logger.info("sale_openid为空，去微信授权页面获取code======");
			String redirect_uri=URLEncoder.encode("http://butupier.jinznet.com/getCode.htm", "UTF-8");
			StringBuffer url=new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Constant.APPID+"&redirect_uri="+redirect_uri+""
					+ "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
			response.sendRedirect(url.toString());
    		return false;  
		}
        return true;  
    }  
      
    @Override  
    public void postHandle(HttpServletRequest request,  
            HttpServletResponse response, Object handler,  
            ModelAndView mv) throws Exception {  
    	
    	String jsApiTicket = CookieUtil.getCookieByKey(request,JSAPITICKET);
		if(StringUtils.isBlank(jsApiTicket)){
			JSONObject object = WXJS.getJsApiTicket();
			jsApiTicket = object.getString("ticket");
			CookieUtil.addCookie(response, JSAPITICKET, jsApiTicket, 60*60, null);
			logger.info("成功存入jsApiTicket到cookie==========" +jsApiTicket );
		}
		
		String url = "";
		url = request.getScheme() +"://" + request.getServerName()  
                        + request.getServletPath();
		if (request.getQueryString() != null){
			url += "?" + request.getQueryString();
		}
		
		Map<String, String> map = WXJS.makeWXTicket(jsApiTicket, url);
		mv.addObject("map",map);
    	
    }  
    
    
  
    @Override  
    public void afterCompletion(HttpServletRequest request,  
            HttpServletResponse response, Object handler, Exception ex)  
    throws Exception {  
          
    }
    
    
    public static void main(String[] args) throws Exception {

		//轻客
		String appid = "wxdf9a3c702eae6131";
		
//		String appid = "wxb3c2625eedd4889d";
		
		String redirect_uri=URLEncoder.encode("http://maikelsong.free.ngrok.cc/getCode.htm", "UTF-8");
		
		StringBuffer url=new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+""
				+ "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
		
		System.out.println(url.toString());
		
	}
    
      
} 